package com.huawei.cloudapp.ui;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.huawei.cloudapp.R;
import com.huawei.cloudapp.common.CASLog;
import com.huawei.cloudapp.common.CasConnectInfo;
import com.huawei.cloudapp.common.CasHistory;
import com.just.agentweb.AgentWeb;
import com.just.agentweb.DefaultWebClient;
import com.just.agentweb.WebChromeClient;

public class CasCloudPhoneWebActivity extends AppCompatActivity {
    private static final String TAG = "CasCloudWebFragment";
    private AgentWeb mAgentWeb;
    private LinearLayout mLinearLayout;
    private CasConnectInfo mConnectInfo;
    public static String urlStr = "https://110.41.19.175:10086/index?"; //h5 sdk部署地址


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cas_cloud_phone_web);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                View.SYSTEM_UI_FLAG_FULLSCREEN);
        mLinearLayout = findViewById(R.id.web_view);

        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(mLinearLayout, -1, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT))
                .useDefaultIndicator(-1, 3)
                .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
                .setWebViewClient(mWebViewClient)
                .setWebChromeClient(mWebChromeClient)
                .setMainFrameErrorView(R.layout.agentweb_error_page, -1)
                .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.DISALLOW)
                .interceptUnkownUrl()
                .createAgentWeb()
                .ready()
                .go(getConnectUrl());
        mAgentWeb.getWebCreator().getWebView().setLayerType(View.LAYER_TYPE_HARDWARE, null);
        mAgentWeb.getJsInterfaceHolder().addJavaObject("CasCallback", new CasCallbackInterface());
        WebSettings webSettings = mAgentWeb.getWebCreator().getWebView().getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setMediaPlaybackRequiresUserGesture(false);
    }

    private com.just.agentweb.WebViewClient mWebViewClient = new com.just.agentweb.WebViewClient();
    private WebChromeClient mWebChromeClient = new WebChromeClient() {
        @Override
        public void onShowCustomView(View view, CustomViewCallback customViewCallback) {
            super.onShowCustomView(view, customViewCallback);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    };

    @Override
    public void onResume() {
        if (mAgentWeb != null) {
            mAgentWeb.getWebLifeCycle().onResume();
        }
        super.onResume();
    }

    @Override
    public void onPause() {
        if (mAgentWeb != null) {
            mAgentWeb.getWebLifeCycle().onPause();
        }
        super.onPause();
    }

    @Override
    public void onDestroy() {
        if (mAgentWeb != null) {
            mAgentWeb.getWebLifeCycle().onDestroy();
        }
        super.onDestroy();
    }


    /**
     * get data with intent
     */
    private String getConnectUrl() {
        CASLog.i(TAG, "getIntentData");
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            mConnectInfo = intent.getParcelableExtra(CasConnectInfo.BUNDLE_KEY);
            return urlStr + "ip=" + mConnectInfo.getConnectIp() + "&port=" + ((Integer.parseInt(mConnectInfo.getConnectPort()) + 1));
        }
        return null;
    }

    class CasCallbackInterface {
        @JavascriptInterface
        public void startSuccessCallback() {
            CasHistory mCasHistory = new CasHistory(getSharedPreferences("input_history", MODE_PRIVATE));
            mCasHistory.setHistory("ip", mConnectInfo.getConnectIp());
            mCasHistory.setHistory("port", mConnectInfo.getConnectPort());
            mCasHistory.setHistory("ip:port", mConnectInfo.getConnectIp() + ":" + mConnectInfo.getConnectPort());
        }

        @JavascriptInterface
        public void exit() {
            CasCloudPhoneWebActivity.this.finish();
        }
    }
}